﻿using RpcCentral.Collect.Model;
using RpcCentral.Common;
using RpcHelper;
using RpcModel;
namespace RpcCentral.Collect.Controller
{
    public class TransmitConfigController : DataSyncClass
    {
        public TransmitConfigController(long systemType, long rpcMerId)
        {
            _RpcMerId = rpcMerId;
            _SystemType = systemType;
        }
        private readonly long _RpcMerId;
        private readonly long _SystemType;
        private string _Ver = string.Empty;
        private volatile int _VerNum = 0;
        private Transmit[] _Transmit;

        protected override void SyncData()
        {
            ITransmitHelper helper= UnityHelper.Resolve<ITransmitHelper>();
            if (helper.GetTransmits(_RpcMerId,_SystemType,ref this._Ver,out this._Transmit))
            {
                _VerNum = Tools.GetRandom();
            }
        }
        public bool CheckVer(ref int ver)
        {
            if (ver != _VerNum)
            {
                ver = _VerNum;
                return true;
            }
            return false;
        }
        public void InitConfig(ServerConfig config)
        {
            ServerTransmit[] list = _Transmit.FindAll(a => a.SeverId == config.ServerId);
            if (list.Length != 0)
            {
                config.Transmit = list;
                config.IsTransmit = true;
            }
        }
        public ServerTransmit[] GetTransmits(long serverId)
        {
            return _Transmit.FindAll(a => a.SeverId == serverId);
        }
    }
}
